package com.uphone.kingmall.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.bean.GoodCommentsBean;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.view.photocheck.PhotoBrowserActivity;

public class CommentsListAdapter extends BaseQuickAdapter<GoodCommentsBean.DataBean, BaseViewHolder> {

    public CommentsListAdapter() {
        super(R.layout.kingmall_item_comment_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, final GoodCommentsBean.DataBean item) {
        ImageView iv_head = helper.getView(R.id.iv_user_head);
        GlideUtils.getInstance().loadNormalImage(mContext, item.getUserPhoto(), iv_head);
        helper.setText(R.id.tv_user_name, item.getUserName() + "");
        helper.setText(R.id.tv_time, item.getCommentTime() + "");
        helper.setText(R.id.tv_goods_info, item.getGoodsSpec() + "");
        helper.setText(R.id.tv_buy_time, item.getBuyTime() + "");
        helper.setText(R.id.tv_content, item.getCommentContent() + "");
        final ImageView ivImage = helper.getView(R.id.iv_image);
        RecyclerView rvImage = helper.getView(R.id.rv_image);
        if (item.getImgs() != null) {
            if (item.getImgs().size() == 1) {
                rvImage.setVisibility(View.GONE);
                ivImage.setVisibility(View.VISIBLE);
                GlideUtils.getInstance().loadCornerImage(MyApplication.mContext, item.getImgs().get(0).getImg(), 7, ivImage);
                ivImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("imageUrls", new String[]{item.getImgs().get(0).getImg()});
                        intent.putExtra("curImageUrl", item.getImgs().get(0).getImg());
                        intent.setClass(mContext, PhotoBrowserActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            } else {
                rvImage.setVisibility(View.VISIBLE);
                ivImage.setVisibility(View.GONE);
                CommentsListImageAdapter commentsListImageAdapter = new CommentsListImageAdapter();
                rvImage.setLayoutManager(new GridLayoutManager(mContext, 3));
                rvImage.setAdapter(commentsListImageAdapter);
                commentsListImageAdapter.setNewData(item.getImgs());
                final String[] images = new String[item.getImgs().size()];
                for (int i = 0; i < item.getImgs().size(); i++) {
                    images[i] = item.getImgs().get(i).getImg();
                }
                commentsListImageAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent();
                        intent.putExtra("imageUrls", images);
                        intent.putExtra("curImageUrl", images[position]);
                        intent.setClass(mContext, PhotoBrowserActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            }
        }
    }
}
